package com.gao.api.gate.quick.core.map;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.*;
import java.util.function.Consumer;

/**
 * <p>
 *     忽略大小写
 * </p>
 *
 * @author gaoshuangwen
 * @since 2022/01/05 16:36
 */
public class IgnoreCaseMultiValueMap<V> implements ListMultimap<String,V> {

    private final ListMultimap<String, V> targetMap;

    private final Set<String> keys = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

    public IgnoreCaseMultiValueMap(ListMultimap<String, V> targetMap) {

        Assert.notNull(targetMap, "'targetMap' must not be null");

        this.targetMap = targetMap;
    }

    @Override
    public int size() {
        return targetMap.size();
    }


    @Override
    public boolean isEmpty() {
        return targetMap.isEmpty();
    }


    @Override
    public boolean containsKey(Object key) {
        return keys.contains(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return targetMap.containsValue(value);
    }

    @Override
    public boolean containsEntry(Object o, Object o1) {
        return targetMap.containsEntry(o,o1);
    }

    @Override
    public boolean put(String s, V v) {

        return put(s,v,s1 -> keys.add(s));
    }

    public boolean put(String s, V v, Consumer<String> keyOp) {
        boolean flag;
        if (StringUtils.isNotBlank(s)) {
            flag =targetMap.put(s.toLowerCase(), v);
        }else {
            flag =targetMap.put(s, v);
        }
        if(flag){
            keyOp.accept(s);
        }
        return flag;
    }

    @Override
    public boolean remove(Object o, Object o1) {
        keys.remove(o);
        return targetMap.remove(o,o1);
    }

    @Override
    public boolean putAll(String s, Iterable<? extends V> iterable) {
        return putAll(s,iterable,s1 -> keys.add(s));
    }

    public boolean putAll(String s, Iterable<? extends V> iterable, Consumer<String> keyOp) {
        boolean flag;
        if (StringUtils.isNotBlank(s)) {
            flag =targetMap.putAll(s.toLowerCase(), iterable);
        }else {
            flag =targetMap.putAll(s, iterable);
        }
        if(flag){
            keyOp.accept(s);
        }
        return flag;
    }

    @Override
    public boolean putAll(Multimap<? extends String, ? extends V> multimap) {
        return false;
    }

    @Override
    public List<V> replaceValues(String s, Iterable<? extends V> iterable) {
        return null;
    }

    @Override
    public List<V> removeAll(Object o) {
        keys.clear();
        return targetMap.removeAll(o);
    }

    @Override
    public void clear() {
        targetMap.clear();
        keys.clear();
    }

    @Override
    public List<V> get(String s) {
        return targetMap.get(s);
    }

    @Override
    public Set<String> keySet() {
        return targetMap.keySet();
    }

    @Override
    public Multiset<String> keys() {
        return targetMap.keys();
    }

    @Override
    public Collection<V> values() {
        return targetMap.values();
    }

    @Override
    public Collection<Map.Entry<String, V>> entries() {
        return targetMap.entries();
    }

    @Override
    public Map<String, Collection<V>> asMap() {
        return targetMap.asMap();
    }


}
